/**
 * Created with IntelliJ IDEA.
 *
 * @author: cxs
 * @date: 2023/7/3
 * @time: 8:47
 * @className：RentController
 * @description： 租车页面
 */
package com.bailan.bus.controller;
import com.bailan.bus.domain.Customer;
import com.bailan.bus.domain.Rent;
import com.bailan.bus.service.ICustomerService;
import com.bailan.bus.service.IRentService;
import com.bailan.bus.vo.CustomerVo;
import com.bailan.bus.vo.RentVo;
import com.bailan.common.util.*;
import com.bailan.sys.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Random;

@RestController
@RequestMapping("rent")
public class RentController {
    @Autowired
    ICustomerService customerService;
    @Autowired
    IRentService rentService;
    /**
     * 检查身份证号是否存在
     * @param rentVo
     * /rent/checkCustomerExist.action
     * @return
     */
    @RequestMapping("checkCustomerExist")
    public ResultObj checkCustomerExist(RentVo rentVo){
        Customer customer=customerService.selectCustomerByIdentity(rentVo.getIdentity());
        if (customer!=null){
            return  ResultObj.STATUS_TRUE;
        }else {
            return ResultObj.STATUS_FALSE;
        }
    }

    /**
     * 初始化添加出租单的表单信息
     * @param rentVo
     * /rent/initRentFrom
     * @return
     */
    @RequestMapping("initRentFrom")
    public RentVo initRentFrom(RentVo rentVo){
        /**
         *设置出租单号
         */
        rentVo.setRentid(RandomUtils.createRandomStringUseTime(SysConstant.CAR_ORDER_CZ));
        /**
         * 设置开始时间
         */
        rentVo.setBegindate(new Date());
        //设置操作员
        User user= (User) WebUtils.getHttpSession().getAttribute("user");
          rentVo.setOpername(user.getRealname());
        return  rentVo;
    }

    /**
     * 保存出租信息
     * @param rentVo
     * /rent/saveRent
     * @return
     */
    @RequestMapping("saveRent")
    public ResultObj saveRent(RentVo rentVo){
        try {
            //设置创建时间
            rentVo.setCreatetime(new  Date());
            //设置归还信息
            rentVo.setRentflag(SysConstant.RENT_BACK_FALSE);
            //保存
            this.rentService.addRent(rentVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 查询出租单
     * @param rentVo
     * /rent/loadAllRent
     * @return
     */
    @RequestMapping("loadAllRent")
    public DataGridView loadAllRent(RentVo rentVo){
        return  this.rentService.queryAllRent(rentVo);
    }

    /**
     * 编辑出租单
     * @param rentVo
     * @return
     */
    @RequestMapping("updateRent")
    public  ResultObj updateRent(RentVo rentVo){
        try{
            this.rentService.updateRent(rentVo);
            return  ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return  ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除出租单信息
     * @param rentVo
     * /rent/deleteRent.action
     * @return
     */
    @RequestMapping("deleteRent")
    @ResponseBody
    public  ResultObj deleteRent(RentVo rentVo){
        try {
            this.rentService.deleteRent(rentVo.getRentid());
            return  ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
    /**
     * 出租单导出
     */
    @RequestMapping("exportRent")
    public ResponseEntity<Object> exportRentMessage(String rentid){
        //获取rentid拿到Rent对象
        Rent rent = rentService.selectRentByRentId(rentid);
        //获取客户信息
        //identity身份证号码
        String identity = rent.getIdentity();
        Customer customer = customerService.selectCustomerByIdentity(identity);
        //设置文件名称
        String fileName = customer.getCustname() + "的出租单信息.xls";
        //设置sheet名称
        String sheetName = customer.getCustname() + "出租单信息";
        //创建流对象，通过工具类
        ByteArrayOutputStream bos = ExportRentUtils.exportRent(rent,customer,sheetName);
        try {
            //文件设置编码
            fileName = URLEncoder.encode(fileName, "UTF-8");
            //创建Head
            HttpHeaders headers = new HttpHeaders();
            //设置Head的Media类型
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            //设置head的attacment文件格式
            headers.setContentDispositionFormData("attachment", fileName);
            //数据封装到ResponseEntity返回
            return new ResponseEntity<>(bos.toByteArray(), headers, HttpStatus.CREATED);
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return null;
    }
}
